package view.overlay;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;

import scheduler.DuplicateEventException;
import scheduler.Notifiable;
import scheduler.Scheduler;
import view.drawing.ImageLibrary;

public class ResetOverlay extends PassiveOverlay {
	
	
	private Image mBackgroundImage;
	
	private double mTransparency;
	
	private double mSpeed;
	
	private Notifiable mFader;
	
	private FadeCallback mCallback;
	
	public ResetOverlay() {
		mBackgroundImage = ImageLibrary.getInstance().getImage("res/images/backgrounds/error.png");
		mSpeed = 0.08f;
		
		mFader = new Notifiable() {

			@Override
			public void execute() {
				mTransparency += mSpeed;
				
				if (mTransparency + mSpeed >= 1.0f) {
					Scheduler.getPersistantClock().cancel(this);
					if (mCallback != null) {
						mCallback.fadeComplete();
					}
				}
				ResetOverlay.this.invalidate();
			}
			
		};
	}
	
	public void setCallback(FadeCallback cb) {
		mCallback = cb;
	}
	
	@Override
	public Dimension getPreferredSize() {
		return (new Dimension(0, 0));
	}
	
	@Override
	public void paint(Graphics g) {
		
		g.drawImage(mBackgroundImage, 0, 0, getWidth(), getHeight(), null);
		
		g.setColor(new Color(0, 0, 0, (int)(Math.max(mTransparency, 0) * 255)));
		
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		
		
	}
	
	public void show() {
		this.setVisible(true);
	}
	
	public void fadeOut() {

		mTransparency = -1.5f;
		
		try {
			Scheduler.getPersistantClock().schedule(mFader, 1);
		} catch (DuplicateEventException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public interface FadeCallback {
		public void fadeComplete();
	}

}
